package com.it.flowable.util;

import org.flowable.bpmn.model.FormProperty;
import org.flowable.bpmn.model.FormValue;

import java.util.ArrayList;
import java.util.List;

public class FormUtils {


    /**
     * 创建枚举类型表单属性
     */
    public static FormProperty createEnumProperty(String id, String name, String... options) {
        FormProperty property = new FormProperty();
        property.setId(id);
        property.setName(name);
        property.setType("enum");
        property.setRequired(true);
        property.setVariable(id);
        property.setFormValues(createFormValues(options));
        // property.getExpression();
        // property.getExtensionElements();
        return property;
    }

    /**
     * 创建日期类型表单属性
     */
    public static FormProperty createDateProperty(String id, String name) {
        FormProperty property = new FormProperty();
        property.setId(id);
        property.setName(name);
        property.setType("date");
        property.setRequired(true);
        property.setVariable(id);
        property.setDatePattern("yyyy-MM-dd");
        return property;
    }

    /**
     * 创建字符串类型表单属性
     */
    public static FormProperty createStringProperty(String id, String name, boolean required) {
        FormProperty property = new FormProperty();
        property.setId(id);
        property.setName(name);
        property.setType("string");
        property.setRequired(required);
        property.setVariable(id);
        return property;
    }

    /**
     * 创建表单枚举值
     */
    public static List<FormValue> createFormValues(String... options) {
        List<FormValue> formValues = new ArrayList<>();
        for (int i = 0; i < options.length; i += 2) {
            FormValue formValue = new FormValue();
            formValue.setId(options[i]);
            formValue.setName(options[i + 1]);
            formValues.add(formValue);
        }
        return formValues;
    }

}
